package userInterface;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.AbstractTableModel;

import model.Item;

/**
 * 
 * @author Igor
 * Copied from ItemsBidTableModel and slightly modified by Daniel Bartholomew
 *
 */

@SuppressWarnings("serial")
public class ItemTableModel extends AbstractTableModel implements ChangeListener {

    private String[] columnNames = {"Item Description", "Min Bid"};
    private List<Item> items = new ArrayList<Item>();

    public ItemTableModel()
    {
    	
    }
    
    public Item getItem(int index)
    {
    	return items.get(index);
    }
    
    public void stateChanged(ChangeEvent arg0) {
        fireTableDataChanged();
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public void add(Item a) {
        if (a != null) {
        	items.add(a);
            fireTableRowsInserted(items.size() - 1,
            		items.size() - 1);

            fireTableDataChanged();

        }
    }

    public void delete(int index) {
    	items.remove(index);

        fireTableDataChanged();

    }

    public int getRowCount() {
        return items.size();
    }

    public Object getValueAt(int row, int col) {
        Object val = null;
        DecimalFormat df = new DecimalFormat("###,###.00");
        switch (col) {
            case 0:
                val = "Item: " + items.get(row).getID() + "\n" +
                items.get(row).getName() + "\n";
                		;
                break;

            case 1:
            	val = df.format(items.get(row).getMinPrice());
                break;

        }
        return val;
    }
}
